package com.fqgj.sentry.controller.pc;

import com.fqgj.sentry.biz.pc.PolicyExecutorBusiness;
import com.fqgj.sentry.biz.pc.vo.policyexecutor.PolicyExecutorHistoryVo;
import com.fqgj.sentry.biz.pc.vo.policyexecutor.PolicyExecutorPendingVo;
import com.fqgj.sentry.biz.pc.vo.policyexecutor.PolicyExecutorVo;
import com.fqgj.sentry.common.annotations.ParamsValidate;
import com.fqgj.sentry.common.response.ApiOutput;
import com.fqgj.sentry.controller.pc.request.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2017/7/19
 * Time: 下午4:58
 */
@RequestMapping("/executor")
@RestController
public class PolicyExecutorController {

    @Autowired
    private PolicyExecutorBusiness policyExecutorBusiness;

    /**
     * 创建  待发布版本
     * @param addPolicyExecutorVo
     * @return
     */
    @ParamsValidate
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiOutput add(@RequestBody AddPolicyExecutorVo addPolicyExecutorVo) {
        PolicyExecutorPendingVo pendingVo = policyExecutorBusiness.addPolicyExecutor(addPolicyExecutorVo);
        return new ApiOutput<>(pendingVo);
    }

    /**
     * 执行者删除
     * @param executorId
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ApiOutput delPolicyExecutor(@RequestParam Long executorId) {
        policyExecutorBusiness.delPolicyExecutor(executorId);
        return new ApiOutput<>();
    }

    /**
     * 执行者 更新
     * @param updateExecutorInfoVo
     * @return
     */
    @RequestMapping(value = "/update/info", method = RequestMethod.POST)
    public ApiOutput updatePolicyExecutorInfo(@RequestBody UpdateExecutorInfoVo updateExecutorInfoVo) {
        policyExecutorBusiness.updatePolicyExecutorInfo(updateExecutorInfoVo);
        return new ApiOutput<>();
    }
    /**
     * 放弃 待发布版本
     *
     * @param executorId
     * @return
     */
    @RequestMapping(value = "/{executorId}/drop", method = RequestMethod.POST)
    public ApiOutput drop(@PathVariable(value = "executorId") Long executorId) {
        policyExecutorBusiness.dropPolicyExecutor(executorId);
        return new ApiOutput<>();
    }


    /**
     * 设置 执行者 开关
     * @param switchVo
     * @return
     */
    @ParamsValidate
    @RequestMapping(value = "/set/switch", method = RequestMethod.POST)
    public ApiOutput setSwitch(@RequestBody ExecutorSwitchVo switchVo) {
        policyExecutorBusiness.setExecutorSwitch(switchVo.getExecutorId(), switchVo.getOpen());
        return new ApiOutput<>();
    }

    /**
     * 设置 百分比
     * @param percentVo
     * @return
     */
    @ParamsValidate
    @RequestMapping(value = "/set/percent", method = RequestMethod.POST)
    public ApiOutput setExecutePercent(@RequestBody ExecutorPercentVo percentVo) {
        policyExecutorBusiness.setChallengerExecutorPercent(percentVo.getExecutorId(), percentVo.getPercent());
        return new ApiOutput<>();
    }

    /**
     * 版本 回滚
     * @param executorId
     * @return
     */
    @RequestMapping(value = "/roll/back", method = RequestMethod.POST)
    public ApiOutput rollBackExecutor(@RequestParam Long executorId) {
        policyExecutorBusiness.rollBackExecutor(executorId);
        return new ApiOutput<>();
    }

    /**
     * 执行者 发布
     *
     * @param executorId
     * @return
     */
    @RequestMapping(value = "/release", method = RequestMethod.POST)
    public ApiOutput releaseExecutor(@RequestParam Long executorId) {
        policyExecutorBusiness.releaseExecutor(executorId);
        return new ApiOutput<>();
    }

    /**
     * 策略集 下的执行者
     * @param policySetsId
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiOutput queryExecutorList(@RequestParam Long policySetsId) {
        Map<String, PolicyExecutorVo> policyExecutorVoMap = policyExecutorBusiness.queryEachTypeExecutor(policySetsId);
        return new ApiOutput<>(policyExecutorVoMap);
    }

    @ParamsValidate
    @RequestMapping(value = "/combo", method = RequestMethod.POST)
    public ApiOutput getExecutorCombo(@RequestBody ExecutorComboVo executorComboVo) {
        List<PolicyExecutorHistoryVo> executorHistoryVos = policyExecutorBusiness.queryExecutorCombo(executorComboVo);
        return new ApiOutput<>(executorHistoryVos);
    }

}
